
//     622. 设计循环队列
public class MyCircularQueue {
    public int[] elem;
    public int front;//队头
    public int rear;//队尾

    public MyCircularQueue(int k) {
        this.elem = new int[k+1];
    }

    /**
     * 入队
     * @param value
     * @return
     */
    public boolean enQueue(int value) {
        if (!isFull()) {
            elem[rear] = value;
//            rear++;//error  因为这样rear一直增加 就不会达到循环的效果
            rear = (rear+1) % elem.length;
            return true;
        }
        return false;
    }

    /**
     * 出队
     * @return
     */
    public boolean deQueue() {
        if (!isEmpty()) {
            front = (front+1) % elem.length;
            return true;
        }
        return false;
    }

    /**
     * 获取到队头元素
     * @return
     */
    public int front() {
        if (isEmpty()) {
//            throw new NullPointerException("队列为空");//在oj上抛异常 不能通过 统一返回-1
            return -1;
        }else {
            return elem[front];
        }
    }

    /**
     * 获取到队尾元素  注意下标elem.length 到 0 下标这部分
     * @return
     */
    public int rear() {
        if (isEmpty()) {
            return -1;
        }else {
            if (rear == 0){
                return elem[elem.length - 1];
            }else {
                return elem[rear-1];
            }
        }
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return ( rear + 1 ) % elem.length == front;
    }
}
